1 /* Copyright (c) 2004-2009, Sara Golemon <sarag@libssh2.org>
2  * Copyright (c) 2009-2012 Daniel Stenberg
3  * Copyright (c) 2010 Simon Josefsson <simon@josefsson.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms,
7  * with or without modification, are permitted provided
8  * that the following conditions are met:
9  *
10  *   Redistributions of source code must retain the above
11  *   copyright notice, this list of conditions and the
12  *   following disclaimer.
13  *
14  *   Redistributions in binary form must reproduce the above
15  *   copyright notice, this list of conditions and the following
16  *   disclaimer in the documentation and/or other materials
17  *   provided with the distribution.
18  *
19  *   Neither the name of the copyright holder nor the names
20  *   of any other contributors may be used to endorse or
21  *   promote products derived from this software without
22  *   specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37  * OF SUCH DAMAGE.
38  */
39 
40 module deimos.libssh2;
41 
42 immutable LIBSSH2_COPYRIGHT = "2004-2012 The libssh2 project and its contributors.";
43 
44 /* We use underscore instead of dash when appending DEV in dev versions just
45    to make the BANNER define (used by src/session.c) be a valid SSH
46    banner. Release versions have no appended strings and may of course not
47    have dashes either. */
48 immutable LIBSSH2_VERSION = "1.4.4_DEV";
49 
50 /* The numeric version number is also available "in parts" by using these
51    defines: */
52 immutable LIBSSH2_VERSION_MAJOR  = 1;
53 immutable LIBSSH2_VERSION_MINOR  = 4;
54 immutable LIBSSH2_VERSION_PATCH  = 4;
55 
56 /* This is the numeric version of the libssh2 version number, meant for easier
57    parsing and comparions by programs. The LIBSSH2_VERSION_NUM define will
58    always follow this syntax:
59 
60          0xXXYYZZ
61 
62    Where XX, YY and ZZ are the main version, release and patch numbers in
63    hexadecimal (using 8 bits each). All three numbers are always represented
64    using two digits.  1.2 would appear as "0x010200" while version 9.11.7
65    appears as "0x090b07".
66 
67    This 6-digit (24 bits) hexadecimal number does not show pre-release number,
68    and it is always a greater number in a more recent release. It makes
69    comparisons with greater than and less than work.
70 */
71 immutable LIBSSH2_VERSION_NUM    = 0x010404;
72 
73 /*
74  * This is the date and time when the full source package was created. The
75  * timestamp is not stored in the source code repo, as the timestamp is
76  * properly set in the tarballs by the maketgz script.
77  *
78  * The format of the date should follow this template:
79  *
80  * "Mon Feb 12 11:35:33 UTC 2007"
81  */
82 immutable LIBSSH2_TIMESTAMP       = "DEV";
83 
84 version(Windows)
85 {
86  import deimos.basetsd;
87  import std.c.windows.winsock;
88 }
89 
90 import std.c.stddef;
91 import std.c.string;
92 import core.stdc.time;
93 
94 version(Windows)
95 {
96   import std.c.windows.stat;
97 }
98 else
99 {
100   import core.sys.posix.sys.stat;
101   import core.sys.posix.sys.types;
102 }
103 
104 // WARN: Darwin and NETWARE support removed
105 
106 alias uint8_t          = ubyte;
107 alias uint32_t         = uint;
108 alias libssh2_uint64_t = ulong;
109 alias libssh2_int64_t  = long;
110 alias ssize_t          = SSIZE_T;
111 
112 version(Windows)
113 {
114   alias libssh2_socket_t           = SOCKET;
115   immutable LIBSSH2_INVALID_SOCKET = INVALID_SOCKET;
116 }
117 else
118 {
119   alias libssh2_socket_t           = int;
120   immutable LIBSSH2_INVALID_SOCKET = -1;
121 }
122 
123 /* Part of every banner, user specified or not */
124 immutable LIBSSH2_SSH_BANNER                   = "SSH-2.0-libssh2_" ~ LIBSSH2_VERSION;
125 
126 /* We *could* add a comment here if we so chose */
127 immutable LIBSSH2_SSH_DEFAULT_BANNER           = LIBSSH2_SSH_BANNER;
128 immutable LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF = LIBSSH2_SSH_DEFAULT_BANNER ~ "\r\n";
129 
130 /* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */
131 immutable LIBSSH2_DH_GEX_MINGROUP = 1024;
132 immutable LIBSSH2_DH_GEX_OPTGROUP = 1536;
133 immutable LIBSSH2_DH_GEX_MAXGROUP = 2048;
134 
135 /* Defaults for pty requests */
136 immutable LIBSSH2_TERM_WIDTH     = 80;
137 immutable LIBSSH2_TERM_HEIGHT    = 24;
138 immutable LIBSSH2_TERM_WIDTH_PX  = 0;
139 immutable LIBSSH2_TERM_HEIGHT_PX = 0;
140 
141 /* 1/4 second */
142 immutable LIBSSH2_SOCKET_POLL_UDELAY   = 250000;
143 /* 0.25 * 120 == 30 seconds */
144 immutable LIBSSH2_SOCKET_POLL_MAXLOOPS = 120;
145 
146 /* Maximum size to allow a payload to compress to, plays it safe by falling
147    short of spec limits */
148 immutable LIBSSH2_PACKET_MAXCOMP = 32000;
149 
150 /* Maximum size to allow a payload to deccompress to, plays it safe by
151    allowing more than spec requires */
152 immutable LIBSSH2_PACKET_MAXDECOMP = 40000;
153 
154 /* Maximum size for an inbound compressed payload, plays it safe by
155    overshooting spec limits */
156 immutable LIBSSH2_PACKET_MAXPAYLOAD = 40000;
157 
158 alias void function(size_t count, void** _abstract)            LIBSSH2_ALLOC_FUNC;
159 alias void function(void* ptr, size_t count, void** _abstract) LIBSSH2_REALLOC_FUNC;
160 alias void function(void* ptr, void** _abstract)               LIBSSH2_FREE_FUNC;
161 
162 struct _LIBSSH2_USERAUTH_KBDINT_PROMPT
163 {
164     char* text;
165     uint length;
166     ubyte echo;
167 } 
168 alias LIBSSH2_USERAUTH_KBDINT_PROMPT = _LIBSSH2_USERAUTH_KBDINT_PROMPT;
169 
170 struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
171 {
172     char* text;
173     uint length;
174 } 
175 alias LIBSSH2_USERAUTH_KBDINT_RESPONSE = _LIBSSH2_USERAUTH_KBDINT_RESPONSE;
176 
177 /* 'publickey' authentication callback */
178 alias int function(LIBSSH2_SESSION* session, char** sig, size_t* sig_len,
179                    const char* data, size_t data_len, void** _abstract) 
180                    LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC;
181 
182 /* 'keyboard-interactive' authentication callback */
183 alias void function(const char* name, int name_len, const char* instruction,
184                     int instruction_len, int num_prompts,
185                     const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
186                     LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, void** _abstract)
187                     LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC;
188 
189 /* Callbacks for special SSH packets */
190 alias void function(LIBSSH2_SESSION* session, const char* message, int message_len,
191                     void** _abstract)
192                     LIBSSH2_IGNORE_FUNC;
193 
194 alias void function(LIBSSH2_SESSION* session, int always_display, const char* message,
195                     int message_len, const char* language, int language_len,
196                     void** _abstract)
197                     LIBSSH2_DEBUG_FUNC;
198 
199 alias void function(LIBSSH2_SESSION* session, int reason, const char* message,
200                     int message_len, const char* language, int language_len,
201                     void** _abstract)
202                     LIBSSH2_DISCONNECT_FUNC;
203 
204 alias void function(LIBSSH2_SESSION* session, char** newpw, int* newpw_len,
205                     void** _abstract)
206                     LIBSSH2_PASSWD_CHANGEREQ_FUNC;
207 
208 alias int function(LIBSSH2_SESSION* session, const char* packet, int packet_len,
209                    void** _abstract)
210                    LIBSSH2_MACERROR_FUNC;
211 
212 alias void function(LIBSSH2_SESSION* session, LIBSSH2_CHANNEL* channel,
213                     const char* shost, int sport, void** _abstract)
214                     LIBSSH2_X11_OPEN_FUNC;
215 
216 alias void function(LIBSSH2_SESSION* session, void** session_abstract,
217                     LIBSSH2_CHANNEL* channel, void** channel_abstract)
218                     LIBSSH2_CHANNEL_CLOSE_FUNC;
219 
220 /* I/O callbacks */
221 alias ssize_t function(libssh2_socket_t socket, void* buffer, size_t length,
222                        int flags, void** _abstract)
223                        LIBSSH2_RECV_FUNC;
224 
225 alias ssize_t function(libssh2_socket_t socket, const void* buffer, size_t length,
226                        int flags, void** _abstract)
227                        LIBSSH2_SEND_FUNC;
228 
229 /* libssh2_session_callback_set() constants */
230 immutable LIBSSH2_CALLBACK_IGNORE     = 0;
231 immutable LIBSSH2_CALLBACK_DEBUG      = 1;
232 immutable LIBSSH2_CALLBACK_DISCONNECT = 2;
233 immutable LIBSSH2_CALLBACK_MACERROR   = 3;
234 immutable LIBSSH2_CALLBACK_X11        = 4;
235 immutable LIBSSH2_CALLBACK_SEND       = 5;
236 immutable LIBSSH2_CALLBACK_RECV       = 6;
237 
238 /* libssh2_session_method_pref() constants */
239 immutable LIBSSH2_METHOD_KEX       = 0;
240 immutable LIBSSH2_METHOD_HOSTKEY   = 1;
241 immutable LIBSSH2_METHOD_CRYPT_CS  = 2;
242 immutable LIBSSH2_METHOD_CRYPT_SC  = 3;
243 immutable LIBSSH2_METHOD_MAC_CS    = 4;
244 immutable LIBSSH2_METHOD_MAC_SC    = 5;
245 immutable LIBSSH2_METHOD_COMP_CS   = 6;
246 immutable LIBSSH2_METHOD_COMP_SC   = 7;
247 immutable LIBSSH2_METHOD_LANG_CS   = 8;
248 immutable LIBSSH2_METHOD_LANG_SC   = 9;
249 
250 /* flags */
251 immutable LIBSSH2_FLAG_SIGPIPE  = 1;
252 immutable LIBSSH2_FLAG_COMPRESS = 2;
253 
254 struct _LIBSSH2_SESSION;
255 struct _LIBSSH2_CHANNEL;
256 struct _LIBSSH2_LISTENER;
257 struct _LIBSSH2_KNOWNHOSTS;
258 struct _LIBSSH2_AGENT;
259 
260 alias LIBSSH2_SESSION    = _LIBSSH2_SESSION;
261 alias LIBSSH2_CHANNEL    = _LIBSSH2_CHANNEL;
262 alias LIBSSH2_LISTENER   = _LIBSSH2_LISTENER;
263 alias LIBSSH2_KNOWNHOSTS = _LIBSSH2_KNOWNHOSTS;
264 alias LIBSSH2_AGENT      = _LIBSSH2_AGENT;
265 
266 struct _LIBSSH2_POLLFD 
267 {
268     char type; /* LIBSSH2_POLLFD_* below */
269 
270     union _fd
271     {
272         libssh2_socket_t socket; /* File descriptors -- examined with
273                                     system select() call */
274         LIBSSH2_CHANNEL *channel; /* Examined by checking internal state */
275         LIBSSH2_LISTENER *listener; /* Read polls only -- are inbound
276                                        connections waiting to be accepted? */
277     };
278     alias fd = _fd;
279 
280     version(Win32)
281     {
282       uint events; /* Requested Events */
283       uint revents; /* Returned Events */
284     }
285     else
286     {
287       ulong events; /* Requested Events */
288       ulong revents; /* Returned Events */
289     }
290 } 
291 alias LIBSSH2_POLLFD = _LIBSSH2_POLLFD;
292 
293 /* Poll FD Descriptor Types */
294 immutable LIBSSH2_POLLFD_SOCKET      = 1;
295 immutable LIBSSH2_POLLFD_CHANNEL     = 2;
296 immutable LIBSSH2_POLLFD_LISTENER    = 3;
297 
298 /* Note: Win32 Doesn't actually have a poll() implementation, so some of these
299    values are faked with select() data */
300 /* Poll FD events/revents -- Match sys/poll.h where possible */
301 immutable LIBSSH2_POLLFD_POLLIN          = 0x0001; /* Data available to be read or
302                                                       connection available --
303                                                       All */
304 immutable LIBSSH2_POLLFD_POLLPRI         = 0x0002; /* Priority data available to
305                                                       be read -- Socket only */
306 immutable LIBSSH2_POLLFD_POLLEXT         = 0x0002; /* Extended data available to
307                                                       be read -- Channel only */
308 immutable LIBSSH2_POLLFD_POLLOUT         = 0x0004; /* Can may be written --
309                                                       Socket/Channel */
310 /* revents only */
311 immutable LIBSSH2_POLLFD_POLLERR         = 0x0008; /* Error Condition -- Socket */
312 immutable LIBSSH2_POLLFD_POLLHUP         = 0x0010; /* HangUp/EOF -- Socket */
313 immutable LIBSSH2_POLLFD_SESSION_CLOSED  = 0x0010; /* Session Disconnect */
314 immutable LIBSSH2_POLLFD_POLLNVAL        = 0x0020; /* Invalid request -- Socket
315                                                       Only */
316 immutable LIBSSH2_POLLFD_POLLEX          = 0x0040; /* Exception Condition --
317                                                       Socket/Win32 */
318 immutable LIBSSH2_POLLFD_CHANNEL_CLOSED  = 0x0080; /* Channel Disconnect */
319 immutable LIBSSH2_POLLFD_LISTENER_CLOSED = 0x0080; /* Listener Disconnect */
320 
321 enum HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION = 1;
322 /* Block Direction Types */
323 immutable LIBSSH2_SESSION_BLOCK_INBOUND                 = 0x0001;
324 immutable LIBSSH2_SESSION_BLOCK_OUTBOUND                = 0x0002;
325 
326 /* Hash Types */
327 immutable LIBSSH2_HOSTKEY_HASH_MD5                          =  1;
328 immutable LIBSSH2_HOSTKEY_HASH_SHA1                         =  2;
329 
330 /* Hostkey Types */
331 immutable LIBSSH2_HOSTKEY_TYPE_UNKNOWN			   = 0;
332 immutable LIBSSH2_HOSTKEY_TYPE_RSA			       = 1;
333 immutable LIBSSH2_HOSTKEY_TYPE_DSS			       = 2;
334 
335 /* Disconnect Codes (defined by SSH protocol) */
336 immutable SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT         = 1;
337 immutable SSH_DISCONNECT_PROTOCOL_ERROR                      = 2;
338 immutable SSH_DISCONNECT_KEY_EXCHANGE_FAILED                 = 3;
339 immutable SSH_DISCONNECT_RESERVED                            = 4;
340 immutable SSH_DISCONNECT_MAC_ERROR                           = 5;
341 immutable SSH_DISCONNECT_COMPRESSION_ERROR                   = 6;
342 immutable SSH_DISCONNECT_SERVICE_NOT_AVAILABLE               = 7;
343 immutable SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED      = 8;
344 immutable SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE             = 9;
345 immutable SSH_DISCONNECT_CONNECTION_LOST                     = 10;
346 immutable SSH_DISCONNECT_BY_APPLICATION                      = 11;
347 immutable SSH_DISCONNECT_TOO_MANY_CONNECTIONS                = 12;
348 immutable SSH_DISCONNECT_AUTH_CANCELLED_BY_USER              = 13;
349 immutable SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE      = 14;
350 immutable SSH_DISCONNECT_ILLEGAL_USER_NAME                   = 15;
351 
352 /* Error Codes (defined by libssh2) */
353 immutable LIBSSH2_ERROR_NONE                     = 0;
354 
355 /* The library once used -1 as a generic error return value on numerous places
356    through the code, which subsequently was converted to
357    LIBSSH2_ERROR_SOCKET_NONE uses over time. As this is a generic error code,
358    the goal is to never ever return this code but instead make sure that a
359    more accurate and descriptive error code is used. */
360 immutable LIBSSH2_ERROR_SOCKET_NONE              = -1;
361 
362 immutable LIBSSH2_ERROR_BANNER_RECV              = -2;
363 immutable LIBSSH2_ERROR_BANNER_SEND              = -3;
364 immutable LIBSSH2_ERROR_INVALID_MAC              = -4;
365 immutable LIBSSH2_ERROR_KEX_FAILURE              = -5;
366 immutable LIBSSH2_ERROR_ALLOC                    = -6;
367 immutable LIBSSH2_ERROR_SOCKET_SEND              = -7;
368 immutable LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE     = -8;
369 immutable LIBSSH2_ERROR_TIMEOUT                  = -9;
370 immutable LIBSSH2_ERROR_HOSTKEY_INIT             = -10;
371 immutable LIBSSH2_ERROR_HOSTKEY_SIGN             = -11;
372 immutable LIBSSH2_ERROR_DECRYPT                  = -12;
373 immutable LIBSSH2_ERROR_SOCKET_DISCONNECT        = -13;
374 immutable LIBSSH2_ERROR_PROTO                    = -14;
375 immutable LIBSSH2_ERROR_PASSWORD_EXPIRED         = -15;
376 immutable LIBSSH2_ERROR_FILE                     = -16;
377 immutable LIBSSH2_ERROR_METHOD_NONE              = -17;
378 immutable LIBSSH2_ERROR_AUTHENTICATION_FAILED    = -18;
379 immutable LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED   = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
380 immutable LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED     = -19;
381 immutable LIBSSH2_ERROR_CHANNEL_OUTOFORDER       = -20;
382 immutable LIBSSH2_ERROR_CHANNEL_FAILURE          = -21;
383 immutable LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED   = -22;
384 immutable LIBSSH2_ERROR_CHANNEL_UNKNOWN          = -23;
385 immutable LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED  = -24;
386 immutable LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED  = -25;
387 immutable LIBSSH2_ERROR_CHANNEL_CLOSED           = -26;
388 immutable LIBSSH2_ERROR_CHANNEL_EOF_SENT         = -27;
389 immutable LIBSSH2_ERROR_SCP_PROTOCOL             = -28;
390 immutable LIBSSH2_ERROR_ZLIB                     = -29;
391 immutable LIBSSH2_ERROR_SOCKET_TIMEOUT           = -30;
392 immutable LIBSSH2_ERROR_SFTP_PROTOCOL            = -31;
393 immutable LIBSSH2_ERROR_REQUEST_DENIED           = -32;
394 immutable LIBSSH2_ERROR_METHOD_NOT_SUPPORTED     = -33;
395 immutable LIBSSH2_ERROR_INVAL                    = -34;
396 immutable LIBSSH2_ERROR_INVALID_POLL_TYPE        = -35;
397 immutable LIBSSH2_ERROR_PUBLICKEY_PROTOCOL       = -36;
398 immutable LIBSSH2_ERROR_EAGAIN                   = -37;
399 immutable LIBSSH2_ERROR_BUFFER_TOO_SMALL         = -38;
400 immutable LIBSSH2_ERROR_BAD_USE                  = -39;
401 immutable LIBSSH2_ERROR_COMPRESS                 = -40;
402 immutable LIBSSH2_ERROR_OUT_OF_BOUNDARY          = -41;
403 immutable LIBSSH2_ERROR_AGENT_PROTOCOL           = -42;
404 immutable LIBSSH2_ERROR_SOCKET_RECV              = -43;
405 immutable LIBSSH2_ERROR_ENCRYPT                  = -44;
406 immutable LIBSSH2_ERROR_BAD_SOCKET               = -45;
407 immutable LIBSSH2_ERROR_KNOWN_HOSTS              = -46;
408 
409 /* this is a define to provide the old (<= 1.2.7) name */
410 alias LIBSSH2_ERROR_BANNER_NONE = LIBSSH2_ERROR_BANNER_RECV;
411 
412 /* Global API */
413 immutable LIBSSH2_INIT_NO_CRYPTO      =  0x0001;
414 
415 extern (C) {
416 nothrow {
417 
418 /*
419  * libssh2_init()
420  *
421  * Initialize the libssh2 functions.  This typically initialize the
422  * crypto library.  It uses a global state, and is not thread safe --
423  * you must make sure this function is not called concurrently.
424  *
425  * Flags can be:
426  * 0:                              Normal initialize
427  * LIBSSH2_INIT_NO_CRYPTO:         Do not initialize the crypto library (ie.
428  *                                 OPENSSL_add_cipher_algoritms() for OpenSSL
429  *
430  * Returns 0 if succeeded, or a negative value for error.
431  */
432 int libssh2_init(int flags);
433 
434 /*
435  * libssh2_exit()
436  *
437  * Exit the libssh2 functions and free's all memory used internal.
438  */
439 void libssh2_exit();
440 
441 /*
442  * libssh2_free()
443  *
444  * Deallocate memory allocated by earlier call to libssh2 functions.
445  */
446 void libssh2_free(LIBSSH2_SESSION *session, void *ptr);
447 
448 /*
449  * libssh2_session_supported_algs()
450  *
451  * Fills algs with a list of supported acryptographic algorithms. Returns a
452  * non-negative number (number of supported algorithms) on success or a
453  * negative number (an eror code) on failure.
454  *
455  * NOTE: on success, algs must be deallocated (by calling libssh2_free) when
456  * not needed anymore
457  */
458 int libssh2_session_supported_algs(LIBSSH2_SESSION* session,
459                                    int method_type,
460                                    const char*** algs);
461 
462 /* Session API */
463 LIBSSH2_SESSION*
464 libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC   my_alloc,
465                         LIBSSH2_FREE_FUNC    my_free,
466                         LIBSSH2_REALLOC_FUNC my_realloc, 
467                         void* _abstract);
468 
469 void** libssh2_session_abstract(LIBSSH2_SESSION* session);
470 
471 void* libssh2_session_callback_set(LIBSSH2_SESSION* session,
472                                    int cbtype, void* callback);
473 int libssh2_session_banner_set(LIBSSH2_SESSION* session,
474                                const char *banner);
475 int libssh2_banner_set(LIBSSH2_SESSION* session,
476                        const char* banner);
477 
478 int libssh2_session_startup(LIBSSH2_SESSION *session, int sock);
479 int libssh2_session_handshake(LIBSSH2_SESSION* session,
480                               libssh2_socket_t sock);
481 int libssh2_session_disconnect_ex(LIBSSH2_SESSION* session,
482                                   int reason,
483                                   const char *description,
484                                   const char *lang);
485 
486 int libssh2_session_free(LIBSSH2_SESSION* session);
487 
488 char* libssh2_hostkey_hash(LIBSSH2_SESSION *session, int hash_type);
489 
490 char* libssh2_session_hostkey(LIBSSH2_SESSION* session,
491                               size_t* len, int* type);
492 
493 int libssh2_session_method_pref(LIBSSH2_SESSION* session,
494                                 int method_type,
495                                 const char* prefs);
496 char* libssh2_session_methods(LIBSSH2_SESSION* session,
497                               int method_type);
498 int libssh2_session_last_error(LIBSSH2_SESSION* session, char** errmsg,
499                                int* errmsg_len, int want_buf);
500 int libssh2_session_last_errno(LIBSSH2_SESSION* session);
501 int libssh2_session_block_directions(LIBSSH2_SESSION* session);
502 
503 int libssh2_session_flag(LIBSSH2_SESSION* session, int flag, int value);
504 char* libssh2_session_banner_get(LIBSSH2_SESSION* session);
505 
506 /* Userauth API */
507 char* libssh2_userauth_list(LIBSSH2_SESSION* session,
508                             const char* username,
509                             uint username_len);
510 int libssh2_userauth_authenticated(LIBSSH2_SESSION* session);
511 
512 int libssh2_userauth_password_ex(LIBSSH2_SESSION* session,
513                                  const char* username,
514                                  uint username_len,
515                                  const char* password,
516                                  uint password_len,
517                                  LIBSSH2_PASSWD_CHANGEREQ_FUNC passwd_change_cb);
518 
519 int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION* session,
520                                            const char* username,
521                                            uint username_len,
522                                            const char *publickey,
523                                            const char *privatekey,
524                                            const char *passphrase);
525 
526 int libssh2_userauth_publickey(LIBSSH2_SESSION* session,
527                                const char* username,
528                                const char* pubkeydata,
529                                size_t pubkeydata_len,
530                                LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC sign_callback,
531                                void** _abstract);
532 
533 int libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION* session,
534                                            const char* username,
535                                            uint username_len,
536                                            const char* publickey,
537                                            const char* privatekey,
538                                            const char* passphrase,
539                                            const char* hostname,
540                                            uint hostname_len,
541                                            const char* local_username,
542                                            uint local_username_len);
543 
544 /*
545  * response_callback is provided with filled by library prompts array,
546  * but client must allocate and fill individual responses. Responses
547  * array is already allocated. Responses data will be freed by libssh2
548  * after callback return, but before subsequent callback invokation.
549  */
550 int libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION* session,
551                                              const char* username,
552                                              uint username_len,
553                                              LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC response_callback);
554 
555 int libssh2_poll(LIBSSH2_POLLFD* fds, uint nfds, long timeout);
556 
557 /* Channel API */
558 immutable LIBSSH2_CHANNEL_WINDOW_DEFAULT  = (2*1024*1024);
559 immutable LIBSSH2_CHANNEL_PACKET_DEFAULT  = 32768;
560 immutable LIBSSH2_CHANNEL_MINADJUST       = 1024;
561 
562 /* Extended Data Handling */
563 immutable LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL      = 0;
564 immutable LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE      = 1;
565 immutable LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE       = 2;
566 
567 immutable SSH_EXTENDED_DATA_STDERR = 1;
568 
569 /* Returned by any function that would block during a read/write opperation */
570 immutable LIBSSH2CHANNEL_EAGAIN = LIBSSH2_ERROR_EAGAIN;
571 
572 LIBSSH2_CHANNEL*
573 libssh2_channel_open_ex(LIBSSH2_SESSION* session, const char* channel_type,
574                         uint channel_type_len,
575                         uint window_size, uint packet_size,
576                         const char* message, uint message_len);
577 
578 LIBSSH2_CHANNEL*
579 libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION* session, const char* host,
580                                 int port, const char* shost, int sport);
581 
582 LIBSSH2_LISTENER*
583 libssh2_channel_forward_listen_ex(LIBSSH2_SESSION* session, const char* host,
584                                   int port, int* bound_port, int queue_maxsize);
585 
586 int libssh2_channel_forward_cancel(LIBSSH2_LISTENER* listener);
587 
588 LIBSSH2_CHANNEL* libssh2_channel_forward_accept(LIBSSH2_LISTENER* listener);
589 
590 int libssh2_channel_setenv_ex(LIBSSH2_CHANNEL* channel,
591                               const char* varname,
592                               uint varname_len,
593                               const char* value,
594                               uint value_len);
595 
596 int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel,
597                                    const char* term,
598                                    uint term_len,
599                                    const char* modes,
600                                    uint modes_len,
601                                    int width, int height,
602                                    int width_px, int height_px);
603 
604 int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL*channel,
605                                         int width, int height,
606                                         int width_px,
607                                         int height_px);
608 
609 int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL* channel,
610                                int single_connection,
611                                const char* auth_proto,
612                                const char* auth_cookie,
613                                int screen_number);
614 
615 int libssh2_channel_process_startup(LIBSSH2_CHANNEL* channel,
616                                     const char* request,
617                                     uint request_len,
618                                     const char* message,
619                                     uint message_len);
620 
621 ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL* channel,
622                                 int stream_id, char *buf,
623                                 size_t buflen);
624 
625 int libssh2_poll_channel_read(LIBSSH2_CHANNEL* channel, int extended);
626 
627 // This may be a recipe for disaster as win32 has long equal to int
628 // Maybe change types
629 ulong libssh2_channel_window_read_ex(LIBSSH2_CHANNEL* channel,
630                                      ulong* read_avail,
631                                      ulong* window_size_initial);
632 
633 /* libssh2_channel_receive_window_adjust is DEPRECATED, do not use! */
634 ulong libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL* channel,
635                                             ulong adjustment,
636                                             char force);
637 
638 int libssh2_channel_receive_window_adjust2(LIBSSH2_CHANNEL* channel,
639                                            ulong adjustment,
640                                            char force,
641                                            uint* storewindow);
642 
643 ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL* channel,
644                                  int stream_id, const char* buf,
645                                  size_t buflen);
646 
647 ulong libssh2_channel_window_write_ex(LIBSSH2_CHANNEL* channel,
648                                       ulong* window_size_initial);
649 
650 void libssh2_session_set_blocking(LIBSSH2_SESSION* session, int blocking);
651 int  libssh2_session_get_blocking(LIBSSH2_SESSION* session);
652 
653 void libssh2_channel_set_blocking(LIBSSH2_CHANNEL* channel, int blocking);
654 
655 void libssh2_session_set_timeout(LIBSSH2_SESSION* session, long timeout);
656 long libssh2_session_get_timeout(LIBSSH2_SESSION* session);
657 
658 /* libssh2_channel_handle_extended_data is DEPRECATED, do not use! */
659 void libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode);
660 int  libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel, int ignore_mode);
661 
662 /* libssh2_channel_ignore_extended_data() is defined below for BC with version
663  * 0.1
664  *
665  * Future uses should use libssh2_channel_handle_extended_data() directly if
666  * LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE is passed, extended data will be read
667  * (FIFO) from the standard data channel
668  */
669 /* DEPRECATED */
670 void libssh2_channel_ignore_extended_data(LIBSSH2_CHANNEL* channel, int ignore)
671 {
672   libssh2_channel_handle_extended_data(channel,
673                                        (ignore) ?
674                                        LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE :
675                                        LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL );
676 }
677 
678 immutable LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA     = -1;
679 immutable LIBSSH2_CHANNEL_FLUSH_ALL               = -2;
680 int libssh2_channel_flush_ex(LIBSSH2_CHANNEL* channel, int streamid);
681 
682 int libssh2_channel_get_exit_status(LIBSSH2_CHANNEL* channel);
683 int libssh2_channel_get_exit_signal(LIBSSH2_CHANNEL* channel,
684                                     char** exitsignal,
685                                     size_t* exitsignal_len,
686                                     char** errmsg,
687                                     size_t* errmsg_len,
688                                     char** langtag,
689                                     size_t* langtag_len);
690 int libssh2_channel_send_eof(LIBSSH2_CHANNEL* channel);
691 int libssh2_channel_eof(LIBSSH2_CHANNEL* channel);
692 int libssh2_channel_wait_eof(LIBSSH2_CHANNEL* channel);
693 int libssh2_channel_close(LIBSSH2_CHANNEL* channel);
694 int libssh2_channel_wait_closed(LIBSSH2_CHANNEL* channel);
695 int libssh2_channel_free(LIBSSH2_CHANNEL* channel);
696 
697 version (Windows)
698 {
699   alias stat_t = struct_stat;
700 }
701 
702 LIBSSH2_CHANNEL* libssh2_scp_recv(LIBSSH2_SESSION* session,
703                                   const char* path,
704                                   stat_t* sb);
705 
706 LIBSSH2_CHANNEL* libssh2_scp_send_ex(LIBSSH2_SESSION*session,
707                                      const char *path, int mode,
708                                      size_t size, long mtime,
709                                      long atime);
710 LIBSSH2_CHANNEL*
711 libssh2_scp_send64(LIBSSH2_SESSION* session, const char* path, int mode,
712                    libssh2_int64_t size, time_t mtime, time_t atime);
713 
714 int libssh2_base64_decode(LIBSSH2_SESSION* session, char** dest,
715                           uint *dest_len, const char *src, uint src_len);
716 
717 char* libssh2_version(int req_version_num);
718 
719 immutable HAVE_LIBSSH2_KNOWNHOST_API = 0x010101; /* since 1.1.1 */
720 immutable HAVE_LIBSSH2_VERSION_API   = 0x010100; /* libssh2_version since 1.1 */
721 
722 struct libssh2_knownhost 
723 {
724     uint magic;  /* magic stored by the library */
725     void* node; /* handle to the internal representation of this host */
726     char* name; /* this is NULL if no plain text host name exists */
727     char* key;  /* key in base64/printable format */
728     int typemask;
729 }
730 
731 /*
732  * libssh2_knownhost_init
733  *
734  * Init a collection of known hosts. Returns the pointer to a collection.
735  *
736  */
737 LIBSSH2_KNOWNHOSTS* libssh2_knownhost_init(LIBSSH2_SESSION* session);
738 
739 /*
740  * libssh2_knownhost_add
741  *
742  * Add a host and its associated key to the collection of known hosts.
743  *
744  * The 'type' argument specifies on what format the given host and keys are:
745  *
746  * plain  - ascii "hostname.domain.tld"
747  * sha1   - SHA1(<salt> <host>) base64-encoded!
748  * custom - another hash
749  *
750  * If 'sha1' is selected as type, the salt must be provided to the salt
751  * argument. This too base64 encoded.
752  *
753  * The SHA-1 hash is what OpenSSH can be told to use in known_hosts files.  If
754  * a custom type is used, salt is ignored and you must provide the host
755  * pre-hashed when checking for it in the libssh2_knownhost_check() function.
756  *
757  * The keylen parameter may be omitted (zero) if the key is provided as a
758  * NULL-terminated base64-encoded string.
759  */
760 
761 /* host format (2 bits) */
762 immutable LIBSSH2_KNOWNHOST_TYPE_MASK    = 0xffff;
763 immutable LIBSSH2_KNOWNHOST_TYPE_PLAIN   = 1;
764 immutable LIBSSH2_KNOWNHOST_TYPE_SHA1    = 2; /* always base64 encoded */
765 immutable LIBSSH2_KNOWNHOST_TYPE_CUSTOM  = 3;
766 
767 /* key format (2 bits) */
768 immutable LIBSSH2_KNOWNHOST_KEYENC_MASK     = (3<<16);
769 immutable LIBSSH2_KNOWNHOST_KEYENC_RAW      = (1<<16);
770 immutable LIBSSH2_KNOWNHOST_KEYENC_BASE64   = (2<<16);
771 
772 /* type of key (2 bits) */
773 immutable LIBSSH2_KNOWNHOST_KEY_MASK     = (7<<18);
774 immutable LIBSSH2_KNOWNHOST_KEY_SHIFT    = 18;
775 immutable LIBSSH2_KNOWNHOST_KEY_RSA1     = (1<<18);
776 immutable LIBSSH2_KNOWNHOST_KEY_SSHRSA   = (2<<18);
777 immutable LIBSSH2_KNOWNHOST_KEY_SSHDSS   = (3<<18);
778 immutable LIBSSH2_KNOWNHOST_KEY_UNKNOWN  = (7<<18);
779 
780 int libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS* hosts,
781                           const char* host,
782                           const char* salt,
783                           const char* key, size_t keylen, int typemask,
784                           libssh2_knownhost** store);
785 
786 /*
787  * libssh2_knownhost_addc
788  *
789  * Add a host and its associated key to the collection of known hosts.
790  *
791  * Takes a comment argument that may be NULL.  A NULL comment indicates
792  * there is no comment and the entry will end directly after the key
793  * when written out to a file.  An empty string "" comment will indicate an
794  * empty comment which will cause a single space to be written after the key.
795  *
796  * The 'type' argument specifies on what format the given host and keys are:
797  *
798  * plain  - ascii "hostname.domain.tld"
799  * sha1   - SHA1(<salt> <host>) base64-encoded!
800  * custom - another hash
801  *
802  * If 'sha1' is selected as type, the salt must be provided to the salt
803  * argument. This too base64 encoded.
804  *
805  * The SHA-1 hash is what OpenSSH can be told to use in known_hosts files.  If
806  * a custom type is used, salt is ignored and you must provide the host
807  * pre-hashed when checking for it in the libssh2_knownhost_check() function.
808  *
809  * The keylen parameter may be omitted (zero) if the key is provided as a
810  * NULL-terminated base64-encoded string.
811  */
812 
813 int libssh2_knownhost_addc(LIBSSH2_KNOWNHOSTS* hosts,
814                            const char* host,
815                            const char* salt,
816                            const char* key, size_t keylen,
817                            const char* comment, size_t commentlen, int typemask,
818                            libssh2_knownhost** store);
819 
820 /*
821  * libssh2_knownhost_check
822  *
823  * Check a host and its associated key against the collection of known hosts.
824  *
825  * The type is the type/format of the given host name.
826  *
827  * plain  - ascii "hostname.domain.tld"
828  * custom - prehashed base64 encoded. Note that this cannot use any salts.
829  *
830  *
831  * 'knownhost' may be set to NULL if you don't care about that info.
832  *
833  * Returns:
834  *
835  * LIBSSH2_KNOWNHOST_CHECK_* values, see below
836  *
837  */
838 
839 immutable LIBSSH2_KNOWNHOST_CHECK_MATCH    = 0;
840 immutable LIBSSH2_KNOWNHOST_CHECK_MISMATCH = 1;
841 immutable LIBSSH2_KNOWNHOST_CHECK_NOTFOUND = 2;
842 immutable LIBSSH2_KNOWNHOST_CHECK_FAILURE  = 3;
843 
844 int libssh2_knownhost_check(LIBSSH2_KNOWNHOSTS *hosts,
845                             const char* host, const char* key, size_t keylen,
846                             int typemask,
847                             libssh2_knownhost** knownhost);
848 
849 /* this function is identital to the above one, but also takes a port
850    argument that allows libssh2 to do a better check */
851 int libssh2_knownhost_checkp(LIBSSH2_KNOWNHOSTS* hosts,
852                              const char* host, int port,
853                              const char* key, size_t keylen,
854                              int typemask,
855                              libssh2_knownhost** knownhost);
856 
857 /*
858  * libssh2_knownhost_del
859  *
860  * Remove a host from the collection of known hosts. The 'entry' struct is
861  * retrieved by a call to libssh2_knownhost_check().
862  *
863  */
864 int libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts, libssh2_knownhost* entry);
865 
866 /*
867  * libssh2_knownhost_free
868  *
869  * Free an entire collection of known hosts.
870  *
871  */
872 void libssh2_knownhost_free(LIBSSH2_KNOWNHOSTS* hosts);
873 
874 /*
875  * libssh2_knownhost_readline()
876  *
877  * Pass in a line of a file of 'type'. It makes libssh2 read this line.
878  *
879  * LIBSSH2_KNOWNHOST_FILE_OPENSSH is the only supported type.
880  *
881  */
882 int libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS* hosts, const char* line, size_t len, int type);
883 
884 /*
885  * libssh2_knownhost_readfile
886  *
887  * Add hosts+key pairs from a given file.
888  *
889  * Returns a negative value for error or number of successfully added hosts.
890  *
891  * This implementation currently only knows one 'type' (openssh), all others
892  * are reserved for future use.
893  */
894 
895 immutable LIBSSH2_KNOWNHOST_FILE_OPENSSH = 1;
896 
897 int libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS* hosts, const char* filename, int type);
898 
899 /*
900  * libssh2_knownhost_writeline()
901  *
902  * Ask libssh2 to convert a known host to an output line for storage.
903  *
904  * Note that this function returns LIBSSH2_ERROR_BUFFER_TOO_SMALL if the given
905  * output buffer is too small to hold the desired output.
906  *
907  * This implementation currently only knows one 'type' (openssh), all others
908  * are reserved for future use.
909  *
910  */
911 int libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS* hosts,
912                                 libssh2_knownhost* known,
913                                 char* buffer, size_t buflen,
914                                 size_t* outlen, /* the amount of written data */
915                                 int type);
916 
917 /*
918  * libssh2_knownhost_writefile
919  *
920  * Write hosts+key pairs to a given file.
921  *
922  * This implementation currently only knows one 'type' (openssh), all others
923  * are reserved for future use.
924  */
925 
926 int libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS* hosts, const char* filename, int type);
927 
928 /*
929  * libssh2_knownhost_get()
930  *
931  * Traverse the internal list of known hosts. Pass NULL to 'prev' to get
932  * the first one. Or pass a poiner to the previously returned one to get the
933  * next.
934  *
935  * Returns:
936  * 0 if a fine host was stored in 'store'
937  * 1 if end of hosts
938  * [negative] on errors
939  */
940 int libssh2_knownhost_get(LIBSSH2_KNOWNHOSTS* hosts,
941                           libssh2_knownhost** store,
942                           libssh2_knownhost* prev);
943 
944 immutable HAVE_LIBSSH2_AGENT_API = 0x010202; /* since 1.2.2 */
945 
946 struct libssh2_agent_publickey 
947 {
948     uint magic;              /* magic stored by the library */
949     void* node;     /* handle to the internal representation of key */
950     char* blob;           /* public key blob */
951     size_t blob_len;               /* length of the public key blob */
952     char* comment;                 /* comment in printable format */
953 }
954 
955 /*
956  * libssh2_agent_init
957  *
958  * Init an ssh-agent handle. Returns the pointer to the handle.
959  *
960  */
961 LIBSSH2_AGENT* libssh2_agent_init(LIBSSH2_SESSION* session);
962 
963 /*
964  * libssh2_agent_connect()
965  *
966  * Connect to an ssh-agent.
967  *
968  * Returns 0 if succeeded, or a negative value for error.
969  */
970 int libssh2_agent_connect(LIBSSH2_AGENT* agent);
971 
972 /*
973  * libssh2_agent_list_identities()
974  *
975  * Request an ssh-agent to list identities.
976  *
977  * Returns 0 if succeeded, or a negative value for error.
978  */
979 int libssh2_agent_list_identities(LIBSSH2_AGENT* agent);
980 
981 /*
982  * libssh2_agent_get_identity()
983  *
984  * Traverse the internal list of public keys. Pass NULL to 'prev' to get
985  * the first one. Or pass a poiner to the previously returned one to get the
986  * next.
987  *
988  * Returns:
989  * 0 if a fine public key was stored in 'store'
990  * 1 if end of public keys
991  * [negative] on errors
992  */
993 int libssh2_agent_get_identity(LIBSSH2_AGENT* agent,
994                                libssh2_agent_publickey** store,
995                                libssh2_agent_publickey* prev);
996 
997 /*
998  * libssh2_agent_userauth()
999  *
1000  * Do publickey user authentication with the help of ssh-agent.
1001  *
1002  * Returns 0 if succeeded, or a negative value for error.
1003  */
1004 int libssh2_agent_userauth(LIBSSH2_AGENT* agent,
1005                            const char* username,
1006                            libssh2_agent_publickey* identity);
1007 
1008 /*
1009  * libssh2_agent_disconnect()
1010  *
1011  * Close a connection to an ssh-agent.
1012  *
1013  * Returns 0 if succeeded, or a negative value for error.
1014  */
1015 int libssh2_agent_disconnect(LIBSSH2_AGENT* agent);
1016 
1017 /*
1018  * libssh2_agent_free()
1019  *
1020  * Free an ssh-agent handle.  This function also frees the internal
1021  * collection of public keys.
1022  */
1023 void libssh2_agent_free(LIBSSH2_AGENT* agent);
1024 
1025 
1026 /*
1027  * libssh2_keepalive_config()
1028  *
1029  * Set how often keepalive messages should be sent.  WANT_REPLY
1030  * indicates whether the keepalive messages should request a response
1031  * from the server.  INTERVAL is number of seconds that can pass
1032  * without any I/O, use 0 (the default) to disable keepalives.  To
1033  * avoid some busy-loop corner-cases, if you specify an interval of 1
1034  * it will be treated as 2.
1035  *
1036  * Note that non-blocking applications are responsible for sending the
1037  * keepalive messages using libssh2_keepalive_send().
1038  */
1039 void libssh2_keepalive_config (LIBSSH2_SESSION* session,
1040                                int want_reply,
1041                                uint interval);
1042 
1043 /*
1044  * libssh2_keepalive_send()
1045  *
1046  * Send a keepalive message if needed.  SECONDS_TO_NEXT indicates how
1047  * many seconds you can sleep after this call before you need to call
1048  * it again.  Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on
1049  * I/O errors.
1050  */
1051 int libssh2_keepalive_send (LIBSSH2_SESSION* session, int* seconds_to_next);
1052 
1053 /* NOTE NOTE NOTE
1054    libssh2_trace() has no function in builds that aren't built with debug
1055    enabled
1056  */
1057 int libssh2_trace(LIBSSH2_SESSION* session, int bitmask);
1058 immutable LIBSSH2_TRACE_TRANS     = (1<<1);
1059 immutable LIBSSH2_TRACE_KEX       = (1<<2);
1060 immutable LIBSSH2_TRACE_AUTH      = (1<<3);
1061 immutable LIBSSH2_TRACE_CONN      = (1<<4);
1062 immutable LIBSSH2_TRACE_SCP       = (1<<5);
1063 immutable LIBSSH2_TRACE_SFTP      = (1<<6);
1064 immutable LIBSSH2_TRACE_ERROR     = (1<<7);
1065 immutable LIBSSH2_TRACE_PUBLICKEY = (1<<8);
1066 immutable LIBSSH2_TRACE_SOCKET    = (1<<9);
1067 
1068 alias void function(LIBSSH2_SESSION*, void*, const char*, size_t) libssh2_trace_handler_func;
1069 int libssh2_trace_sethandler(LIBSSH2_SESSION* session, void* context,
1070                              libssh2_trace_handler_func callback);
1071 } // nothrow
1072 } // extern (C)
1073 
1074 /*
1075  * Implementation of convenience functions
1076  */
1077 LIBSSH2_SESSION* libssh2_session_init() { return libssh2_session_init_ex(null, null, null, null); }
1078 
1079 int libssh2_session_disconnect(LIBSSH2_SESSION* session, const char* description)
1080 {
1081   return libssh2_session_disconnect_ex(session, SSH_DISCONNECT_BY_APPLICATION, description, "");
1082 }
1083 
1084 int libssh2_userauth_password(LIBSSH2_SESSION* session, const char* username, 
1085                               const char* password)
1086 {
1087   return libssh2_userauth_password_ex(session, username, strlen(username), password, strlen(password), null);
1088 }
1089 
1090 int libssh2_userauth_publickey_fromfile(LIBSSH2_SESSION* session, 
1091                                         const char* username, 
1092                                         const char* publickey,
1093                                         const char* privatekey, 
1094                                         const char* passphrase) 
1095 {
1096   return libssh2_userauth_publickey_fromfile_ex(session, username, strlen(username), publickey, privatekey, passphrase);
1097 }
1098 
1099 int libssh2_userauth_hostbased_fromfile(LIBSSH2_SESSION* session, 
1100                                         const char* username,
1101                                         const char* publickey,
1102                                         const char* privatekey,
1103                                         const char* passphrase,
1104                                         const char* hostname,
1105                                         const char* local_username)
1106 {
1107   return libssh2_userauth_hostbased_fromfile_ex (session, username, strlen(username), publickey, privatekey, passphrase, 
1108                                                  hostname, strlen(hostname), local_username, strlen(local_username));
1109 }
1110 
1111 int libssh2_userauth_keyboard_interactive(LIBSSH2_SESSION* session,
1112                                           const char* username,
1113                                           LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC response_callback)
1114 {
1115   return libssh2_userauth_keyboard_interactive_ex(session, username, strlen(username), response_callback);
1116 }
1117 
1118 LIBSSH2_CHANNEL*
1119 libssh2_channel_open_session(LIBSSH2_SESSION* session, const char* channel_type,
1120                              uint channel_type_len, uint window_size, 
1121                              uint packet_size, const char* message)
1122 {
1123   return libssh2_channel_open_ex(session, "session", strlen("session"),
1124                                  LIBSSH2_CHANNEL_WINDOW_DEFAULT,
1125                                  LIBSSH2_CHANNEL_PACKET_DEFAULT, null, 0);
1126 }
1127 
1128 LIBSSH2_CHANNEL*
1129 libssh2_channel_direct_tcpip(LIBSSH2_SESSION* session, const char* host, int port)
1130 {
1131   return libssh2_channel_direct_tcpip_ex(session, host, port, "127.0.0.1", 22);
1132 }
1133 
1134 LIBSSH2_LISTENER*
1135 libssh2_channel_forward_listen(LIBSSH2_SESSION* session, int port)
1136 {
1137   return libssh2_channel_forward_listen_ex(session, null, port, null, 16);
1138 }
1139 
1140 int libssh2_channel_setenv(LIBSSH2_CHANNEL* channel,
1141                            const char* varname,
1142                            const char* value)
1143 {
1144   return libssh2_channel_setenv_ex(channel, varname, strlen(varname), value, strlen(value));
1145 }
1146 
1147 
1148 int libssh2_channel_request_pty(LIBSSH2_CHANNEL *channel,
1149                                 const char* term)
1150 {
1151   return libssh2_channel_request_pty_ex(channel, term, strlen(term), null, 0,
1152                                         LIBSSH2_TERM_WIDTH, LIBSSH2_TERM_HEIGHT,
1153                                         LIBSSH2_TERM_WIDTH_PX, LIBSSH2_TERM_HEIGHT_PX);
1154 }
1155 
1156 int libssh2_channel_request_pty_size(LIBSSH2_CHANNEL*channel,
1157                                      int width, int height)
1158 {
1159   return libssh2_channel_request_pty_size_ex(channel, width, height, 0, 0);
1160 }
1161 
1162 int libssh2_channel_x11_req(LIBSSH2_CHANNEL* channel,
1163                             int screen_number)
1164 {
1165   return libssh2_channel_x11_req_ex(channel, 0, null, null, screen_number);
1166 }
1167 
1168 int libssh2_channel_shell(LIBSSH2_CHANNEL* channel)
1169 {
1170   return libssh2_channel_process_startup(channel, "shell", strlen("shell"), null, 0);
1171 }
1172 
1173 int libssh2_channel_exec(LIBSSH2_CHANNEL* channel, const char* command)
1174 {
1175   return libssh2_channel_process_startup(channel, "exec", strlen("shell"), command, strlen(command));
1176 }
1177 
1178 int libssh2_channel_subsystem(LIBSSH2_CHANNEL* channel, const char* subsystem)
1179 {
1180   return libssh2_channel_process_startup(channel, "subsystem", strlen("subsystem"), subsystem, strlen(subsystem));
1181 }
1182 
1183 ssize_t libssh2_channel_read(LIBSSH2_CHANNEL* channel, char *buf, size_t buflen)
1184 {
1185   return libssh2_channel_read_ex(channel, 0, buf, buflen);
1186 }
1187 
1188 ssize_t libssh2_channel_read_stderr(LIBSSH2_CHANNEL* channel, char *buf, size_t buflen)
1189 {
1190   return libssh2_channel_read_ex(channel, SSH_EXTENDED_DATA_STDERR, buf, buflen);
1191 }
1192 
1193 ulong libssh2_channel_window_read(LIBSSH2_CHANNEL* channel)
1194 {
1195   return libssh2_channel_window_read_ex(channel, null, null);
1196 }
1197 
1198 ssize_t libssh2_channel_write(LIBSSH2_CHANNEL* channel, const char* buf, size_t buflen)
1199 {
1200   return libssh2_channel_write_ex(channel, 0, buf, buflen);
1201 }
1202 
1203 ssize_t libssh2_channel_write_stderr(LIBSSH2_CHANNEL* channel, const char* buf, size_t buflen)
1204 {
1205   return libssh2_channel_write_ex(channel, SSH_EXTENDED_DATA_STDERR, buf, buflen);
1206 }
1207 
1208 ulong libssh2_channel_window_write(LIBSSH2_CHANNEL* channel)
1209 {
1210   return libssh2_channel_window_write_ex(channel, null);
1211 }
1212 
1213 int libssh2_channel_flush(LIBSSH2_CHANNEL* channel)
1214 {
1215   return libssh2_channel_flush_ex(channel, 0);
1216 }
1217 
1218 int libssh2_channel_flush_stderr(LIBSSH2_CHANNEL* channel)
1219 {
1220   return libssh2_channel_flush_ex(channel, SSH_EXTENDED_DATA_STDERR);
1221 }
1222 
1223 // TODO There was a libssh2_int64_t instead of size_t for path. But that does
1224 //      not make sense if ..._send_ex takes size_t as argument for 32bit program
1225 LIBSSH2_CHANNEL*
1226 libssh2_scp_send(LIBSSH2_SESSION* session, const char* path, int mode,
1227                  size_t size)
1228 {
1229   return libssh2_scp_send_ex(session, path, mode, size, 0, 0);
1230 }